package com.xlh.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.xlh.Utils.Constant;
import com.xlh.Utils.FUti;
import com.xlh.bean.TitleBean;
import com.xlh.interf.IActivity;
import com.xlh.interf.IAdapter;
import com.xlh.outside.R;

import java.util.List;

public class FloatRightButListAdapter extends BaseAdapter implements IAdapter{

	private LayoutInflater mInflater;
	private List<TitleBean> datas;

	//MyAdapter需要一个Context，通过Context获得Layout.inflater，然后通过inflater加载item的布局
	public FloatRightButListAdapter(Context context, List<TitleBean> datas) {

		mInflater = LayoutInflater.from(context);
		this.datas = datas;
	}

	@Override
	public <T> void addData(T data) {
		if(this.datas==null) return;
		this.datas.add((TitleBean) data);
	}

	@Override
	public <T> IAdapter setDatas(List<T> datas) {
		this.datas = (List<TitleBean>)datas;
		return this;
	}

	@Override
	public void update() {
		this.notifyDataSetChanged();
	}

	//返回数据集的长度
	@Override
	public int getCount() {
		return datas.size();
	}

	@Override
	public void clear() {
		if(this.datas!=null){
			this.datas.clear();
			this.notifyDataSetChanged();
		}
	}

	@Override
	public Object getItem(int position) {
		return datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	//这个方法才是重点，我们要为它编写一个ViewHolder
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.float_but_item, parent, false); //加载布局
			holder = new ViewHolder();

			holder.titleTv = (TextView) convertView.findViewById(R.id.title_text);
			convertView.setTag(holder);
		} else {   //else里面说明，convertView已经被复用了，说明convertView中已经设置过tag了，即holder
			holder = (ViewHolder) convertView.getTag();
		}

		TitleBean bean = datas.get(position);
		holder.titleTv.setText(bean.getmText());


//		GradientDrawable drawable = (GradientDrawable) holder.titleTv.getBackground();
//		//设置边框
//		drawable.setStroke(FUti.dip2px(convertView.getContext(),1), Color.parseColor(Constant.getAppColor().getButBorderColor()));
//		//设置背景
//		drawable.setColor(Color.parseColor(Constant.getAppColor().getButBackColor()));


		//半透明背景
		//holder.titleTv.getBackground().setAlpha(200);
		holder.titleTv.setTextColor(Color.parseColor(Constant.getAppColor().getFontColor() ));


		holder.titleTv.getLayoutParams().width = FUti.dip2px(convertView.getContext(),70);
		holder.titleTv.getLayoutParams().height = FUti.dip2px(convertView.getContext(),35);

		holder.titleTv.setText(bean.getmText());


		return convertView;
	}

	//这个ViewHolder只能服务于当前这个特定的adapter，因为ViewHolder里会指定item的控件，不同的ListView，item可能不同，所以ViewHolder写成一个私有的类
	private class ViewHolder {
		TextView titleTv;

	}
	
}
